﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Generator.Helpers
{
    /// <summary>
    /// Ce classe représente un Helper pour traiter les fichiers
    /// </summary>
    internal class FileHelper
    {
        #region File Attribute Method
        /// <summary>
        /// Removes the read only attribute.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        internal static void RemoveReadOnlyAttribute(string fileName) {
            File.SetAttributes(fileName, FileAttributes.Normal);
        }
        #endregion

        #region Directory Attribute Method
        #endregion

        #region CheckFilename Method

        /// <summary>
        /// Vérifier 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        internal bool CheckFilename(string filename)
        {
            return !filename.EndsWith(".user", StringComparison.InvariantCultureIgnoreCase) &&
                 !filename.EndsWith(".gpState", StringComparison.InvariantCultureIgnoreCase) &&
                 !filename.EndsWith(".sln", StringComparison.InvariantCultureIgnoreCase) &&
                 !filename.EndsWith(".suo", StringComparison.InvariantCultureIgnoreCase) &&
                 !filename.EndsWith(".xap", StringComparison.InvariantCultureIgnoreCase) && // TODO ! Ne prends pas en compte le XAP
                 !filename.EndsWith(VSTemplateHelper.PROJECT_EXTENSION, StringComparison.InvariantCultureIgnoreCase);
        } 

        #endregion

        #region CheckFilenameExtension Method

        /// <summary>
        /// Vérifier 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        internal bool CheckFilenameExtension(string filename)
        {
            FileInfo file = new FileInfo(filename);
            List<string> extensions = new List<string>() { ".cs", ".txt", ".xaml", ".html", ".aspx", ".htm", ".xml", ".xml", ".svc", ".svc", ".config" }; 
            return extensions.Exists(e => e.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase));
        }

        #endregion
        
        #region CheckFilenameSilverlightCore Method

        /// <summary>
        /// Vérifier que le fichier est le fichier Core Silverlight application
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        internal bool CheckFilenameSilverlightCore(string filename)
        {
            var coreFilenames = ".Silverlight.Core.csproj";
            return filename.EndsWith(coreFilenames, StringComparison.InvariantCultureIgnoreCase);
        } 

        #endregion
                
        #region CheckFilenameWebSite Method

        /// <summary>
        /// Vérifier que le fichier est le fichier Web Application
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        internal bool CheckFilenameWebSite(string filename)
        {
            var coreFilenames = ".WebSite.csproj";
            return filename.EndsWith(coreFilenames, StringComparison.InvariantCultureIgnoreCase);
        } 

        #endregion

        #region CheckFolder Method

        internal bool CheckFolder(string folder)
        {
            return !folder.Equals("Bin", StringComparison.InvariantCultureIgnoreCase) &&
                 !folder.Equals("obj", StringComparison.InvariantCultureIgnoreCase) &&
                 !folder.Equals(".svn", StringComparison.InvariantCultureIgnoreCase);
        } 

        #endregion

        #region CheckProjectFile Method
        
        internal bool CheckProjectFile(string filename)
        {
            return filename.EndsWith(VSTemplateHelper.PROJECT_EXTENSION, StringComparison.InvariantCultureIgnoreCase);
        } 

        #endregion

        #region CheckContainProjectFile Method
        
        internal bool CheckContainProjectFile(string folder, out string projectFilename)
        {
            projectFilename = Directory.GetFiles(folder).ToList().FirstOrDefault(f => this.CheckProjectFile(f));

            return !string.IsNullOrEmpty(projectFilename);
        } 

        #endregion

        #region ShortFilename Method
        
        public string ShortFilename(string filename)
        {
            return filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - filename.LastIndexOf('\\') - 1);
        } 

        #endregion

        #region ShortFolder Method
        
        public string ShortFolder(string folder)
        {
            return folder.Substring(folder.LastIndexOf('\\') + 1, folder.Length - folder.LastIndexOf('\\') - 1);
        } 

        #endregion

    }

}
